How to define Calculated fields

The Standards module Field Definition Record Type1 is used to calculate and assign a value for any field in any module:

A Field Definition record includes code that is executed whenever a record is created or modified, the result of which is a value that is assigned to a field immediately before the record is saved. This makes it possible to define your own Summary Data, Extended Data or indeed any field calculations without the need to have Axiell modify the database forms.

The Calculated Registry entry is used to control which fields have calculated values. Support is provided for both single and multiple value fields. Only fields listed in a Calculated Registry entry will have calculated values, and all other fields will continue to use the EMu default value generator.

To define a calculated field:

  1. Create a new record in the Standards module and select Field Definition from the Record Type drop list:

    Field Definition

  2. On the Field tab we name the field definition and define the module and field whose value is to be calculated. In Script Fields we select one or more fields whose values will be available to the script code when calculating the field value:

    Here we see an example Field tab for the Summary Data field in the Thesaurus module:

    Field Definition

  3. The Script tab is used to define a perl subroutine (fieldFormat) for calculating the field value. It includes two fields:

    Code

    Field

    Details

    Script

    Enter a perl subroutine to calculate the field value.

    Tip: If a Preview Registry entry has been defined for this Field Definition (named in Name: (Field Name)), the Preview button will be enabled. Click the button to preview the calculated field value.

    An empty perl script subroutine called fieldFormat is added to the Script field when a Field Definition record is created:

    Modify the subroutine to produce the field value. The desired field value is set using the return statement. In this example, return ""; sets the field value to be empty.

    A return value of the perl undef leaves the field value untouched.

    Notes:
    • Strict: the code for the subroutine must comply with perl’s strict module and all variables used must be strictly defined. As such, each variable definition should be preceded with the reserved word my.
    • Container: the code for the subroutine runs within a container so that the execution of the code only allows value calculation and does not allow access to any perl functions that interact with the machine (operating system calls or file functions, for example).
    Predefined variables

    The following perl variables are available for access in the script code:

    @fields

    perl list of Script Fields values.

    $json

    reference to a perl hash of Script Fields values.

    $luts

    reference to a perl hash containing system level Lookup List values.

    $langdelim

    delimiter used between different languages.

    $langcount

    number of languages supported.

    $ENV{dateformat}

    the date format string used in your EMu implementation.

    $ENV{timeformat}

    the time format string used in your EMu implementation.

    Each of these predefined variables is covered below:

    Errors

    A read-only list of errors identified in the perl routine. All errors must be resolved before the field value can be generated.